﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using Tools;
using Tour.DAL;

namespace Tour.BLL
{
    /// <summary>
    /// Summary description for TblConfig
    /// </summary>
    public class TblConfigManagerBase
    {
        public TblConfigManagerBase()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public void InsertUpdate(TblConfigEntity item)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", item.Id));
            ls.Add(new SqlParameter("@TextID", item.TextID));
            ls.Add(new SqlParameter("@Title", item.Title));
            ls.Add(new SqlParameter("@Content", item.Content));
            ls.Add(new SqlParameter("@LanguageId", item.LanguageId));
            ls.Add(new SqlParameter("@DisplayOrder", item.DisplayOrder));
            ls.Add(new SqlParameter("@CreatedBy", item.CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", item.CreatedDate));
            ls.Add(new SqlParameter("@UpdatedBy", item.UpdatedBy));
            ls.Add(new SqlParameter("@UpdatedDay", item.UpdatedDay));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblConfig", sqlparam);
        }
        public void InsertUpdate(int Id, string TextID, string Title, string Content, int LanguageId, int DisplayOrder, string CreatedBy, System.DateTime CreatedDate, string UpdatedBy, System.DateTime UpdatedDay)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            ls.Add(new SqlParameter("@TextID", TextID));
            ls.Add(new SqlParameter("@Title", Title));
            ls.Add(new SqlParameter("@Content", Content));
            ls.Add(new SqlParameter("@LanguageId", LanguageId));
            ls.Add(new SqlParameter("@DisplayOrder", DisplayOrder));
            ls.Add(new SqlParameter("@CreatedBy", CreatedBy));
            ls.Add(new SqlParameter("@CreatedDate", CreatedDate));
            ls.Add(new SqlParameter("@UpdatedBy", UpdatedBy));
            ls.Add(new SqlParameter("@UpdatedDay", UpdatedDay));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_InsertUpdateTblConfig", sqlparam);
        }
        public string GetUniqueTextIdFromUnicodeText(string UnicodeText)
        {
            string strSql = @"  SELECT TextId FROM TblConfig tn
                                WHERE tn.TextId=@TextId";
           string textId = WebUtility.GetUniqueTextIdFromUnicodeText(UnicodeText, "@TextId", strSql
               , SqlHelper.TourConnectString, 255);
           return textId;
        }
        public void Delete(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            SqlHelper.ExecuteNonQuery(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_DeleteTblConfig", sqlparam);
        }
        public TblConfigEntity GetTblConfigEntity(DataRow dr)
        {
            TblConfigEntity item = new TblConfigEntity();            
            if (dr.Table.Columns.Contains("Id"))
                item.Id = dr["Id"].ToInt();              
            if (dr.Table.Columns.Contains("TextID"))
                item.TextID = dr["TextID"].ToString();              
            if (dr.Table.Columns.Contains("Title"))
                item.Title = dr["Title"].ToString();              
            if (dr.Table.Columns.Contains("Content"))
                item.Content = dr["Content"].ToString();
            if (dr.Table.Columns.Contains("LanguageId"))
                item.LanguageId = dr["LanguageId"].ToInt();
            if (dr.Table.Columns.Contains("DisplayOrder"))
                item.DisplayOrder = dr["DisplayOrder"].ToInt();  
            if (dr.Table.Columns.Contains("CreatedBy"))
                item.CreatedBy = dr["CreatedBy"].ToString();              
            if (dr.Table.Columns.Contains("CreatedDate"))
                item.CreatedDate = dr["CreatedDate"].ToString().ToDateTime("vi-VN");              
            if (dr.Table.Columns.Contains("UpdatedBy"))
                item.UpdatedBy = dr["UpdatedBy"].ToString();              
            if (dr.Table.Columns.Contains("UpdatedDay"))
                item.UpdatedDay = dr["UpdatedDay"].ToString().ToDateTime("vi-VN");              
            return item;
        }        
        public DataTable SelectOneRDT(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblConfig", sqlparam);
        }
        public TblConfigEntity SelectOne(int Id)
        {
            List<SqlParameter> ls = new List<SqlParameter>();
            ls.Add(new SqlParameter("@Id", Id));
            SqlParameter[] sqlparam = ls.ToArray();
            DataTable dt = SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblConfig", sqlparam);
            if(dt!=null && dt.Rows.Count>0)
                return GetTblConfigEntity(dt.Rows[0]);
            return null;
        }
        public DataTable SelectAllRDT()
        {
            return SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblConfigsAll", null);
        }
        public List<TblConfigEntity> SelectAll()
        {
            List<TblConfigEntity> lsresult= new List<TblConfigEntity>();
            DataTable dt =  SqlHelper.ExecuteDataTable(SqlHelper.TourConnectString, CommandType.StoredProcedure, "tr_SelectTblConfigsAll", null);
            if (dt != null && dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    lsresult.Add(GetTblConfigEntity(dt.Rows[i]));            
            return lsresult;
        }
    }
}

